//
//  SportDetailViewController.m
//  MansApp
//
//  Created by lanou3g on 15/7/14.
//  Copyright (c) 2015年 ACH. All rights reserved.
//

#import "SportDetailViewController.h"
#import "SportPieceView.h"
#import "SportDetailCollectionViewCell.h"

@interface SportDetailViewController ()<UIWebViewDelegate,UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout>
//@property(nonatomic,strong)UIWebView *webView;
@property(nonatomic,strong)UICollectionView *collectionView;
@property(nonatomic,copy)NSString *myContent;
@end

@implementation SportDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    

    NSLog(@"my url%@",self.full_url);
    
// [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"{{%$I9FPUELGKD89L8)OL)W.jpg"] forBarMetrics:UIBarMetricsDefault];
    
    self.view.backgroundColor = [UIColor whiteColor];
   

    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    self.collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, kWidth, kHeight) collectionViewLayout:layout];
    
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    self.collectionView.backgroundColor = [UIColor whiteColor];
    
    [self.view addSubview:self.collectionView];
    


    
//    UIButton *button =  [UIButton buttonWithType:UIButtonTypeSystem];
//    button.frame = CGRectMake(0, kHeight - 40, kWidth, 40);
//    [button setBackgroundColor:[UIColor greenColor]];
//    [button addTarget:self action:@selector(buttonAction) forControlEvents:UIControlEventTouchUpInside];
//    [self.view addSubview: button];


}

- (NSInteger )collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    
    return 1;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
     static NSString *cellIdentifer = @"item";
    [collectionView registerClass:[SportDetailCollectionViewCell class] forCellWithReuseIdentifier:cellIdentifer];
    SportDetailCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifer forIndexPath:indexPath];
    
     cell.webView.delegate = self;
    
    [self AsynchronousURL:self.full_url webView:cell.webView];
    
    cell.headView.titleLabel.text = self.title1;
    cell.headView.autherLabel.text = self.auther;
    
  
    [cell.button addTarget:self action:@selector(buttonAction) forControlEvents:UIControlEventTouchUpInside];
   
    return cell;
    
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    return CGSizeMake(kWidth, kHeight);
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{
    
    return 0;
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{
    
    return 0;
}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
    
    
    return UIEdgeInsetsMake(0, 0, 0, 0);
    
}
- (void)buttonAction{
    
    [self dismissViewControllerAnimated:NO completion:nil];
    
}

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{
    
    NSLog(@"~~~~~~~~%@",request);
    NSLog(@"========%ld",(long)navigationType);
    
    return YES;
}

- (void)webViewDidFinishLoad:(UIWebView *)webView{
    
    // 禁用用户选择
    [webView stringByEvaluatingJavaScriptFromString:@"document.documentElement.style.webkitUserSelect='none';"];
    
    // 禁用长按弹出框
    [webView stringByEvaluatingJavaScriptFromString:@"document.documentElement.style.webkitTouchCallout='none';"];
}

- (void)viewWillAppear:(BOOL)animated{
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)AsynchronousURL:(NSString *)url webView:(UIWebView *)webView{
    
        NSMutableArray *imageArray = [NSMutableArray array];
       [ACHNetWork GET:self.full_url success:^(id response) {
    
           NSDictionary *dict = response[@"data"];
           NSArray *array = dict[@"media"];
           for (NSDictionary *obj in array) {
               NSString *str = obj[@"url"];
               [imageArray addObject:str];
           }
           NSString *content = dict[@"content"];
    
           int i = 0;
           while ([content containsString:kImagePlaceholder]) {
    
               NSRange range = [content rangeOfString:kImagePlaceholder];
               content = [content stringByReplacingCharactersInRange:range withString:imageArray[i]];
               i++;
    
               if (i >= imageArray.count) {
                   break;
               }
    
           }
    
    
           NSString *op = @"http://www.myzaker.com/?_zkcmd=open_media&index=0";
    
     
           content = [content stringByReplacingOccurrencesOfString:op withString:@"www.baidu.com"];
    
           [webView loadHTMLString:content baseURL:nil];
           
       } failure:nil];
   
    
    
    
    
    
    
    
    
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
